<script setup lang="ts">
import {User, Lock} from '@element-plus/icons-vue'
import {reactive, computed,ref} from 'vue'
import {reqLogin} from "@/api/user/user.js";
import useUserStore from '@/store/user/user.js'
import {useRouter} from 'vue-router'
const router = useRouter()
const userStore = useUserStore()
const loginForm = reactive({
  username:'',
  password:'',
})
const rules = []
const loading = ref(false)
const getTime = () => {
  let message:string = ''
  let hours = new Date().getHours()
  if (hours < 10) {
    message='早上'
  } else if (hours <= 12) {
    message = '上午'
  } else if (hours < 18) {
    message = '下午'
  } else {
    message = '晚上'
  }
  return message
}
const clickLogin = async() => {
  loading.value = true
  try{
    await userStore.login(loginForm)
    await router.push('/')
    loading.value = false
    ElNotification({
      type:'success',
      message: '欢迎回来',
      title: `Hi,${getTime()}好`,
    })
  } catch(e){
    ElNotification({
      type:'error',
      message:(e as Error).message,
    })
    loading.value = false
  }
}

</script>

<template>
<div class="login-container">
  <el-row>
    <el-col :span="12" :xs="0">
    </el-col>
    <el-col :span="12" :xs="24" style="display:flex;justify-content: space-around">
      <el-form class="login_form" :model="loginForm" >
        <div>
          <h1>Hello,World!</h1>
          <h2>欢迎！</h2>
        </div>
        <el-form-item>
          <el-input type="text" :prefix-icon="User" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input type="password" :prefix-icon="Lock" v-model="loginForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item style="display: flex;justify-content:space-around">
          <el-button :loading="loading" type="primary" size="large" class="login-btn" @click = 'clickLogin'>登录</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</div>
</template>

<style scoped lang="scss">
.login-container {
  width: 100vw;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat ;
  background-size: cover;
}
.login_form {
  background: url('@/assets/images/login_form.png') no-repeat ;
  background-size: cover;
  width:60%;
  position: relative;
  top:30vh;
  padding:40px;
  .login-btn{
    width:100%;
  }
}
</style>